import { Injectable } from '@angular/core';
import {Observable} from 'rxjs';
import {HttpClient} from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class RedisService {

  apiBase = "http://localhost:9000/gateway/user";

  constructor(private http: HttpClient) { }

  /**
   * 添加bloom过滤器
   * */
  addBloom(param: String): Observable<boolean>{
    const url = this.apiBase + "/redis/bloom?key=" + param;
    return this.http.get<boolean>(url);
  }

  /**
   * 调用限流接口
   * */
  allRequest(){
    const url = this.apiBase + "/redis/allowRequest";
    return this.http.get<boolean>(url);
  }

  /**
   * 锁操作
   * */
  normal(key:String){
    const url = this.apiBase + "/lock/normal/" + key;
    return this.http.get<boolean>(url);
  }
  limit(key:String){
    const url = this.apiBase + "/lock/limit/" + key;
    return this.http.get<boolean>(url);
  }
  lock(key:String){
    const url = this.apiBase + "/lock/lock/" + key;
    return this.http.get<boolean>(url);
  }
  tryLock(key:String){
    const url = this.apiBase + "/lock/try/" + key;
    return this.http.get<boolean>(url);
  }
  tryLockTime(key:String){
    const url = this.apiBase + "/lock/tryTime/" + key;
    return this.http.get<boolean>(url);
  }

  /**
   * springboot 集成 redis 缓存
   * */
  cacheable(username:String){
    const url = this.apiBase + "/redis/demo/cacheable/" + username;
    return this.http.get<boolean>(url);
  }
  cacheEvent(user:{}){
    const url = this.apiBase + "/redis/demo/cacheEvent";
    return this.http.post(url, user);
  }

  /**
   * 缓存问题接口
   * */
  search(username:String){
    const url = this.apiBase + "/redis/demo/question/" + username;
    return this.http.get(url);
  }
  editDelay(user:{}){
    const url = this.apiBase + "/redis/demo/question/double/write/delay";
    return this.http.post(url, user);
  }
  editLock(user:{}){
    const url = this.apiBase + "/redis/demo/question/double/write/lock";
    return this.http.post(url, user);
  }
}
